package com.jd.dlc.limit.biz;

import com.jd.dlc.domain.WrapLimiterRule;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

/**
 * @program: dlc
 * @ClassName KeyFactory
 * @description:
 * @author: sizegang
 * @create: 2022-06-11
 **/
@Slf4j
@Component
public class KeyProcessor {

    /**
     * @param
     * @return
     */
    public String wrapperKey(String key, WrapLimiterRule wrapLimiterRule) {
        if (StringUtils.isBlank(key)) {
            return null;
        }
        try {
            if (wrapLimiterRule.getTruncateStart() != null && wrapLimiterRule.getTruncateEnd() != null) {
                return key.substring(wrapLimiterRule.getTruncateStart(), wrapLimiterRule.getTruncateEnd());

            }
            if (wrapLimiterRule.getTruncateStart() != null && wrapLimiterRule.getTruncateEnd() == null) {
                return key.substring(wrapLimiterRule.getTruncateStart());
            }
        } catch (Exception ex) {
            log.error("wrapperKey  key {} error{}", key, ex);
        }
        return key;
    }

}
